package command;

import java.net.InetAddress;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.*;
import command.Command;
import command.CommandException;
import db.CommonConnect;

import common.CommonUtil;
import common.Crypto;
import common.EDUBOX_Baese64_Crypto;
import common.UserSession;
import db.QueryManager;
import db.DAO;

/**
 * Command 인터페이스를 implement 하여 구현된 command.
 */
public class IdChkCommand extends Command {

	private String next;
	
	public IdChkCommand(String next) {
		this.next=next;
	}

	/**
	 * 실제 요청을 받아 처리하는 메소드
	 * 요청을 받으면  요청을 처리하고 next 페이지를 돌려줌
	 * 
	 * @param req HttpServletRequest
	 * @throws CommandException
	 * @return next 이동할 페이지
	 */
	public String execute(HttpServletRequest req)
	throws CommandException {
		ArrayList list =  null;
		HashMap listMap = new HashMap();	
	
		Connection con = null;  
		try {		
			String gubun = CommonUtil.nvl(req.getParameter("gubun"));
			String ent_alias = CommonUtil.nvl(req.getParameter("ent_alias"));
			String id = CommonUtil.nvl(Crypto.EncodeBySType(req.getParameter("id")));
			String ent_id = CommonUtil.nvl(req.getParameter("ent_id"));
			
			String num = CommonUtil.nvl(req.getParameter("num"));	
			String code = CommonUtil.nvl(req.getParameter("code"));	
			
			String queryStr = null;
			
			con = (new CommonConnect()).getConnection_ds1();
			con.setAutoCommit(false);
			String[] param=null;
			
			if(gubun.equals("admin")){
				logger.debug("gubun ==> "+gubun+"    id ==> "+id);
				
				queryStr = QueryManager.getQuery("sp_id_chk.query");
				param= new String[2];
				param[0] = gubun;	
				param[1] = id;	
				
				logger.debug("queryStr ==> "+"sp_id_chk.query");
			}else if(gubun.equals("ent")){
				logger.debug("gubun ==> "+gubun+"    ent_alias ==> "+ent_alias);
				
				queryStr = QueryManager.getQuery("sp_id_chk.query");
				param= new String[2];
				param[0] = gubun;	
				param[1] = ent_alias;	
				
				logger.debug("queryStr ==> "+"sp_id_chk.query");
			}else if(gubun.equals("member")){
				logger.debug("gubun ==> "+gubun+"    id ==> "+id +"    ent_id ==> "+ent_id);
				
				queryStr = QueryManager.getQuery("sp_member_id_chk.query");
				param= new String[3];
				param[0] = gubun;	
				param[1] = id;	
				param[2] = ent_id;
				
				logger.debug("queryStr ==> "+"sp_member_id_chk.query");
			}else if(gubun.equals("tsexam")){
				logger.debug("gubun ==> "+gubun+"    num ==> "+num +"    code ==> "+code);
				
				queryStr = QueryManager.getQuery("sp_member_id_chk.query");
				param= new String[3];
				param[0] = gubun;	
				param[1] = num;	
				param[2] = code;
				
				logger.debug("queryStr ==> "+"sp_member_id_chk.query");
			}
			
			list = DAO.select(con,queryStr,param);
			
			listMap.put("mainContent",list);
					     
		     
// 리퀘스트에 모두 담는다.
			req.setAttribute("list_map",listMap);

		}catch (Exception ex) {
			try{
				con.rollback();
			}catch(Exception ee){}
			ex.printStackTrace();
			throw new CommandException(ex.getMessage());
		}finally {
			try{
				if (con!=null) con.setAutoCommit(true);
				if (con!=null) con.close();
			}catch(Exception ee){}
		}
		return next;
	}
	


}
